package com.qianfeng.bob.xiexiaobo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qianfeng.bob.xiexiaobo.R;
import com.qianfeng.bob.xiexiaobo.adapter.HomeFragementAdapter;
import com.qianfeng.bob.xiexiaobo.bean.HomeFragmentDatas;
import com.qianfeng.bob.xiexiaobo.config.NetConfig;
import com.qianfeng.bob.xiexiaobo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bob on 2015/12/16.
 * 这是内容的Fragment
 */
public class HomeContentFragment extends  BaseFragment {
    private PullToRefreshListView mListView;
    private int mRefreshPosition = 1;
    private List<HomeFragmentDatas.DataEntity.ItemsEntity> mDatas;
    private HomeFragementAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initData();
        initView(view);
        setData();
        setListener();
        loadDatas(mRefreshPosition);
        return view ;
    }

    private void setData() {
        mAdapter = new HomeFragementAdapter(getActivity(),mDatas);
        mListView.setAdapter(mAdapter);
    }

    private void initData(){
        mDatas = new ArrayList<>();
    }
    private void loadDatas(int position){
        if (position==1){
            mDatas.clear();
            System.out.println("mRefreshPosition " +mRefreshPosition);
            mRefreshPosition = position;
        }
            mRefreshPosition ++;
        System.out.println(" mDatas.clear()===="+NetConfig.HOMEFRAGMENT_DATAS+mRefreshPosition);

        getString(NetConfig.HOMEFRAGMENT_DATAS+mRefreshPosition , new StringCallBack() {
            @Override
            public void getString(String content) {
                Gson gson = new Gson();
                HomeFragmentDatas data = gson.fromJson(content,HomeFragmentDatas.class);
                mDatas.addAll(data.getData().getItems());
                mAdapter.notifyDataSetChanged();
                mListView.onRefreshComplete();

            }

            @Override
            public void getStringField() {
                mListView.onRefreshComplete();
                ToastUtils.showLongTimeToast("网络异常请检查网络",getActivity());
            }
        });


    }

    private void setListener() {
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        mListView.setPullLabel("下拉刷新");
        mListView.setReleaseLabel("放开开始刷新");
        mListView.setRefreshingLabel("正在为您拼命加载.....");
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadDatas(1);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
               loadDatas(mRefreshPosition);
            }
        });
    }

    private void initView(View view) {
        mListView = ((PullToRefreshListView) view.findViewById(R.id.lv_homefragment_show));
    }
}
